package by.alex.dzeshko.facebooksuperclient.adapter;

import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.api.ApiRequest;
import by.alex.dzeshko.facebooksuperclient.image.ImageLoader;
import by.alex.dzeshko.facebooksuperclient.models.FbNewsFeed;

public class CommentsAdapter extends ArrayAdapter<JSONObject> {

	public CommentsAdapter(Context context, int textViewResourceId,	List<JSONObject> objects) {
		super(context, textViewResourceId, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		JSONObject jo = getItem(position);
		FbNewsFeed comment = new FbNewsFeed(jo);
		if (v == null) {
			v = View.inflate(parent.getContext(), R.layout.adapter_comments, null);
			ViewHolder holder = new ViewHolder();
			holder.commenterName = (TextView) v.findViewById(R.id.tvCommenterName);
			holder.message = (TextView) v.findViewById(R.id.tvComment);
			holder.imageView = (ImageView) v.findViewById(R.id.ivCommenterImage);
			v.setTag(holder);
		}
		ViewHolder holder = (ViewHolder) v.getTag();
		holder.commenterName.setText(comment.getFromName());
		holder.message.setText(comment.getMessage());
		String imUrl = ApiRequest.getProfilePicture(comment.getFromId(), ApiRequest.SQUARE);
		if (!TextUtils.isEmpty(imUrl)){
			holder.imageView.setImageResource(R.drawable.com_facebook_profile_default_icon);
			ImageLoader.getInstance().bind(this, holder.imageView, imUrl);
		}
		return v;
	}

	static class ViewHolder{
		TextView commenterName, message;
		ImageView imageView;
	}

}
